package statistics.demo.statistics_demo;

import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;

import com.github.mikephil.charting.charts.BarChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.data.BarData;
import com.github.mikephil.charting.data.BarDataSet;
import com.github.mikephil.charting.data.BarEntry;
import com.github.mikephil.charting.interfaces.datasets.IBarDataSet;

import java.util.ArrayList;

public class CaloriesActivity extends BaseActivity {

    private ImageView mBackBtn;
    private ImageView mShareBtn;
    private BarChart mBarChart;
    private String[] mHours = new String[] {
            "0时",
            "1时",
            "2时",
            "3时",
            "4时",
            "5时",
            "6时",
            "7时",
            "8时",
            "9时",
            "10时",
            "11时",
            "12时",
            "13时",
            "14时",
            "15时",
            "16时",
            "17时",
            "18时",
            "19时",
            "20时",
            "21时",
            "22时",
            "23时",
            "24时",
    };

    @Override
    protected void initView(Bundle savedInstanceState) {
        setContentView(R.layout.activity_calories);
        mBackBtn = getViewById(R.id.caloriedBack);
        mShareBtn = getViewById(R.id.caloriedShare);
        mBarChart = getViewById(R.id.caloriesChart);
    }

    @Override
    protected void setListener() {
        mBackBtn.setOnClickListener(this);
        mShareBtn.setOnClickListener(this);
    }

    @Override
    protected void processLogic(Bundle savedInstanceState) {

        /**
         * 图表数据绑定
         * 具体设置可参考 https://github.com/PhilJay/MPAndroidChart
         */

        mBarChart.getDescription().setEnabled(false);

        // if more than 60 entries are displayed in the mBarChart, no values will be
        // drawn
        mBarChart.setMaxVisibleValueCount(60);

        // scaling can now only be done on x- and y-axis separately
        mBarChart.setPinchZoom(false);

        mBarChart.setDrawBarShadow(false);
        mBarChart.setDrawGridBackground(false);


        XAxis xAxis = mBarChart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(true);
        xAxis.setDrawAxisLine(true);
        xAxis.setGranularity(5f);
        xAxis.setAxisMinimum(0.6f);
        xAxis.setAxisMaximum((float) (mHours.length - 0.6));
        xAxis.setLabelCount(5, true);

        HoursAxisValueFormatter formatter = new HoursAxisValueFormatter();
        xAxis.setValueFormatter(formatter);


        mBarChart.getAxisRight().setDrawLabels(false);

        mBarChart.getAxisLeft().setDrawGridLines(false);

        // add a nice and smooth animation
        mBarChart.animateY(1500);

        mBarChart.getLegend().setEnabled(false);

        ArrayList<BarEntry> values = new ArrayList<>();

        for (int i = 0; i < mHours.length; i++) {
            float multi = (mHours.length);
            float val = (float) (Math.random() * multi) + multi / 3;
            values.add(new BarEntry(i, val));
        }
        BarDataSet set1 = new BarDataSet(values, "Data Set");
        set1.setColors(Color.parseColor("#FAA543"),Color.parseColor("#FAA543"));;
        set1.setDrawValues(false);
        ArrayList<IBarDataSet> dataSets = new ArrayList<>();
        dataSets.add(set1);

        BarData data = new BarData(dataSets);
        mBarChart.setData(data);
        mBarChart.setFitBars(true);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.caloriedBack:
                this.finish();
                break;
            case R.id.caloriedShare:
                // TODO: 分享
                break;
        }
    }
}
